/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
<template>
  <el-dialog
    title="计费配置"
    v-if="closeDialog"
    :visible.sync="closeDialog"
    append-to-body
    width="440px"
  >
    <m-popover
      ref="popover"
      @close="close"
      @ok="_ok"
      minWidth="400px"
    >
      <template slot="content">
        <div class="box border">
          <div class="title">
            基本信息
          </div>
          <el-form
            ref="form"
            label-width="120px"
            size="mini"
            label-position="left"
          >
            <el-form-item label="资源编号">
              <span>{{info.productNo||'-'}}</span>
            </el-form-item>
            <el-form-item label="资源名称">
              <span>{{info.productName||'-'}}</span>
            </el-form-item>
            <el-form-item label="资源规格">
              <div>vCPU: {{`${info.vCpu}核`}}</div>
              <div>内存: {{`${toGB(info.memory)}GB`}}</div>
              <div>存储: {{`${toGB(info.dataTotal)}GB`}}</div>
            </el-form-item>
            <el-form-item label="资源类型">
              <span>{{info.productType===1?'虚拟资源':info.productType===2?'模型':info.productType===0?'物理资源':'-'}}</span>
            </el-form-item>
            <el-form-item label="来源">
              <span>平台 (admin)</span>
            </el-form-item>
            <el-form-item label="部署时间">
              <span>{{info.deployTime}}</span>
            </el-form-item>
          </el-form>
        </div>
        <div class="box">
          <div class="title">
            计费配置
          </div>
          <el-form
            ref="form"
            :model="form"
            label-width="120px"
            size="mini"
            label-position="left"
            :rules="rules"
          >
            <el-form-item
              label="计费规则"
              prop="billRule"
            >
              <el-radio-group v-model="form.billRule">
                <el-radio :label="0">标准计费</el-radio>
                <el-radio :label="1">梯度计费</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item
              label="计费类型"
              prop="billType"
            >
              <el-select
                v-model="form.billType"
                placeholder="请选择"
                :disabled="true"
                style="width:100%"
              >
                <el-option
                  label="按量计费"
                  :value="0"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              label="单价(元/分钟)"
              prop="price"
            >
              <el-input-number
                v-model="form.price"
                size="mini"
                :min="0"
                :max="999999.99"
                :controls="false"
                :precision="2"
                style="width:100%"
              ></el-input-number>
            </el-form-item>
            <el-form-item
              label="平台服务费比例"
              prop="serviceRatio"
            >
              <el-input-number
                :disabled="true"
                v-model="form.serviceRatio"
                size="mini"
                :min="0"
                :max="999999"
                :controls="false"
                :precision="0"
                style="width:100%"
              ></el-input-number>
            </el-form-item>
          </el-form>
        </div>
      </template>
    </m-popover>
  </el-dialog>
</template>
<script>
  import { getProductInfo, putProductInfo } from '@/conf/home/apis'
  import mPopover from '@/module/components/popup/popover'
  export default {
    name: 'close-dialog',
    data () {
      return {
        closeDialog: false,
        item: null,
        form: {
          id: '',
          billRule: 0,
          billType: 0,
          price: null,
          serviceRatio: 0
        },
        info: {},
        rules: {
          billRule: [
            { required: true, message: '请选择计费规则', trigger: 'blur' },
          ],
          billType: [
            { required: true, message: '请选择计费规则', trigger: 'blur' },
          ],
          price: [
            { required: true, message: '请输入单价', trigger: 'blur' },
          ],
          serviceRatio: [
            { required: true, message: '请输入平台服务费比例', trigger: 'blur' },
          ],
        }
      }
    },
    methods: {
      toGB (count) {
        return (count / 1024 / 1024 / 1024).toFixed(1) || 0
      },
      show (item) {
        this.item = item
        if (this.item.id) {
          this.getInfo(this.item.id)
        }
        this.closeDialog = true
      },
      async getInfo (id) {
        try {
          const res = await getProductInfo(id)
          if (res && res.success) {
            const { billRule, billType, price, serviceRatio, id } = res.data
            this.info = res.data
            this.form.id = id
            if (billRule !== null) {
              this.form = Object.assign({}, this.form, {
                billRule,
                billType,
                price,
                serviceRatio
              })
              console.log('this.form', this.form)
            }
          } else {
            this.$message.warning(res.msg || '')
          }
        } catch (error) {
          console.log('error===>', error)
        }
      },
      _ok () {
        this.$refs.form.validate(async (valid) => {
          if (!valid) return
          try {
            const res = await putProductInfo(this.form)
            if (res && res.success) {
              this.$emit('update')
              this.$message.success('配置完成')
              this.close()
            } else {
              this.$message.warning(res.msg || '')
            }
          } catch (error) {
            console.log('error===>', error)
          }
        })
      },
      close () {
        this.item = null
        this.form = {
          billRule: 0,
          billType: 0,
          price: null,
          serviceRatio: 0
        }
        this.$refs.form.clearValidate()
        this.closeDialog = false
      }
    },
    created () {

    },
    mounted () {
    },
    components: { mPopover }
  }
</script>
<style lang="scss" scoped>
/deep/.border {
  border-bottom: 1px dashed #dadada;
  padding-top: 8px !important;
  .el-form-item--mini.el-form-item {
    margin-bottom: 3px !important;
  }
}
/deep/.el-input-number .el-input__inner {
  text-align: left;
}
.box {
  padding: 0 20px;
  padding-top: 15px;
  .title {
    width: 64px;
    height: 20px;
    font-size: 16px;
    font-family: PingFang SC, PingFang SC-600;
    font-weight: 600;
    text-align: LEFT;
    color: #333333;
    line-height: 20px;
    margin-bottom: 10px;
  }
}
/deep/.el-form-item--mini.el-form-item {
  margin-bottom: 15px;
}
</style>
